package com.danton.auth.validator;

import java.util.Enumeration;

import com.danton.common.bean.CommonResponse;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

/**
 * RegisterValidator.
 */
public class LoginValidator extends Validator {
	
	protected void validate(Controller controller) {
		validateRequiredString("user_name", "err_user_name", "user_name can not be null;");
		validateRequiredString("password", "err_password", "password can not be null;");
		validateRequiredString("access_key_id", "err_access_key_id", "access_key_id can not be null;");
		validateRequiredString("access_key_secret", "err_access_key_secret", "access_key_secret can not be null;");
	}
	
	protected void handleError(Controller controller) {
		StringBuilder sb = new StringBuilder();
		for (Enumeration<String> e = controller.getAttrNames(); e.hasMoreElements();) {
			String err_key = e.nextElement();
			if (err_key.startsWith("err_")) {
				sb.append(controller.getAttrForStr(err_key));
			}
		}
		CommonResponse resStatus = CommonResponse.buildResponse(600, sb.toString());
		controller.renderJson(resStatus);
	}
}
